package com.wk.kxexamadminister.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wk.kxexamadminister.entity.Student;
import com.wk.kxexamadminister.entity.StudentExt;
import com.wk.kxexamadminister.mapper.StudentMapper;
import com.wk.kxexamadminister.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(description = "Student操作")
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;

    @ApiOperation("增加")
    @PostMapping("/addStudent")
    public String addStudent(Student student){
        int insert = studentMapper.insert(student);
        if(insert == 1){
            return "redirect:/findAllStudents";
        }else {
            return "error";
        }
    }

    @ApiOperation("删除")
    @GetMapping("/delStudent")
    @ResponseBody
    public String delStudentById(Integer sNum){
        int del = studentMapper.deleteById(sNum);
        if(del == 1){
            return "Success";
        }else {
            return "Failure";
        }
    }
    @ApiOperation("修改")
    @PostMapping("/updStudent")
    public String updStudentById(Student student){
        int i = studentMapper.updateById(student);
        if(i == 1){
            return "redirect:/findAllStudents";
        }else {
            return "error";
        }
    }

    @ApiOperation("根据学号查询")
    @PostMapping("/findStudent")
    @ResponseBody
    public Student findStudentByNum(int sNum){
        return studentMapper.selectById(sNum);
    }



    @ApiOperation("查询全部")
    @GetMapping("/findAllStudents")          // mybatis分页插件
    public String findAll(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum, 1);

        List<StudentExt> allStudent = studentMapper.findAll();

        PageInfo<StudentExt> studentExtPageInfo = new PageInfo<>(allStudent);

        model.addAttribute("allStudent",allStudent);
        model.addAttribute("page",studentExtPageInfo);

        return "13-queryAllStudent";
    }






}
